<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>



          <el-form-item label="孩子数量" prop="childrenNum" label-width="150">
            <el-input v-model="queryParams.childrenNum" placeholder="请输入孩子数量" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>

          <el-form-item label="是否做过志愿者" prop="isVolunteer" label-width="150">
            <el-select v-model="queryParams.isVolunteer" placeholder="请选择是否做过志愿者" clearable>
              <el-option
                v-for="dict in whether_or_not"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否加入过社会组织" prop="isSociety" label-width="150">
            <el-select v-model="queryParams.isSociety" placeholder="请选择是否加入过社会组织" clearable>
              <el-option
                v-for="dict in whether_or_not"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否做过志愿者" prop="societyInfo" label-width="150">
            <el-select v-model="queryParams.societyInfo" placeholder="请选择是否做过志愿者" clearable>
              <el-option
                v-for="dict in whether_or_not"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否有信仰" prop="isBelief">
            <el-select v-model="queryParams.isBelief" placeholder="请选择是否有信仰" clearable>
              <el-option
                v-for="dict in whether_or_not"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="信仰内容" prop="belief">
            <el-input v-model="queryParams.belief" placeholder="请输入信仰内容" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="提供的帮助" prop="helpNeeds" label-width="100">
            <el-input v-model="queryParams.helpNeeds" placeholder="请输入提供的帮助" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="帮助详情" prop="helpNeedsInfo">
            <el-input v-model="queryParams.helpNeedsInfo" placeholder="请输入帮助详情" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:volunteer:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:volunteer:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:volunteer:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:volunteer:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="volunteerList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户ID" align="center" prop="userId" v-if="false" />
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="性别" align="center" prop="sex" />
        <el-table-column label="年龄" align="center" prop="age" />
        <el-table-column label="民族" align="center" prop="nationality" />
        <el-table-column label="联系方式" align="center" prop="tel" />
        <el-table-column label="工作单位" align="center" prop="workUnit" />
        <el-table-column label="政治面貌" align="center" prop="politicalStatus" />
        <el-table-column label="家庭所在区" align="center" prop="community"  width="100px" />
        <el-table-column label="婚姻状况" align="center" prop="maritalStatus" />
        <el-table-column label="孩子性别" align="center" prop="childrenSex">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.childrenSex"/>
          </template>
        </el-table-column>
        <el-table-column label="孩子数量" align="center" prop="childrenNum" />
        <el-table-column label="是否注册为企事业单位" align="center" prop="isEnterprises"   width="150px"/>
        <el-table-column label="是否做过志愿者" align="center" prop="isVolunteer"  width="150px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isVolunteer"/>
          </template>
        </el-table-column>
        <el-table-column label="是否加入过社会组织" align="center" prop="isSociety"  width="150px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isSociety"/>
          </template>
        </el-table-column>
        <el-table-column label="是否做过志愿者" align="center" prop="societyInfo"  width="120px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.societyInfo"/>
          </template>
        </el-table-column>
        <el-table-column label="是否对中华优秀传统文化感兴趣" align="center" prop="isTradition"  width="150px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isTradition"/>
          </template>
        </el-table-column>
        <el-table-column label="哪本中华优秀传统文化书籍对自己有较大影响" align="center" prop="traditionInfo"  width="150px"/>
        <el-table-column label="是否有信仰" align="center" prop="isBelief" width="100px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isBelief"/>
          </template>
        </el-table-column>
        <el-table-column label="信仰内容" align="center" prop="belief" />
        <el-table-column label="提供的帮助" align="center" prop="helpNeeds"  width="100px"/>
        <el-table-column label="帮助详情" align="center" prop="helpNeedsInfo" />
        <el-table-column label="对于帮扶同一个孩子的志愿者能否接受线下聚会" align="center" prop="isParty"  width="150px">
          <template #default="scope">
            <dict-tag :options="whether_or_not" :value="scope.row.isParty"/>
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center" prop="picture" />
        <el-table-column label="状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="business_information_zt" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:volunteer:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:volunteer:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改志愿者详细信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="volunteerFormRef" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="民族" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="工作单位" prop="workUnit">
          <el-input v-model="form.workUnit" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="家庭所在区" prop="community">
          <el-input v-model="form.community" placeholder="请输入家庭所在区" />
        </el-form-item>
        <el-form-item label="孩子性别" prop="childrenSex">
          <el-select v-model="form.childrenSex" placeholder="请选择孩子性别">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="孩子数量" prop="childrenNum">
          <el-input v-model="form.childrenNum" placeholder="请输入孩子数量" />
        </el-form-item>
        <el-form-item label="是否注册为企事业单位" prop="isEnterprises">
          <el-input v-model="form.isEnterprises" placeholder="请输入是否注册为企事业单位" />
        </el-form-item>
        <el-form-item label="是否做过志愿者" prop="isVolunteer">
          <el-select v-model="form.isVolunteer" placeholder="请选择是否做过志愿者">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否加入过社会组织" prop="isSociety">
          <el-select v-model="form.isSociety" placeholder="请选择是否加入过社会组织">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否做过志愿者" prop="societyInfo">
          <el-select v-model="form.societyInfo" placeholder="请选择是否做过志愿者">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否对中华优秀传统文化感兴趣" prop="isTradition">
          <el-select v-model="form.isTradition" placeholder="请选择是否对中华优秀传统文化感兴趣">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="哪本中华优秀传统文化书籍对自己有较大影响" prop="traditionInfo">
          <el-input v-model="form.traditionInfo" placeholder="请输入哪本中华优秀传统文化书籍对自己有较大影响" />
        </el-form-item>
        <el-form-item label="是否有信仰" prop="isBelief">
          <el-select v-model="form.isBelief" placeholder="请选择是否有信仰">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信仰内容" prop="belief">
          <el-input v-model="form.belief" placeholder="请输入信仰内容" />
        </el-form-item>
        <el-form-item label="提供的帮助" prop="helpNeeds">
          <el-input v-model="form.helpNeeds" placeholder="请输入提供的帮助" />
        </el-form-item>
        <el-form-item label="帮助详情" prop="helpNeedsInfo">
          <el-input v-model="form.helpNeedsInfo" placeholder="请输入帮助详情" />
        </el-form-item>
        <el-form-item label="对于帮扶同一个孩子的志愿者能否接受线下聚会" prop="isParty">
          <el-select v-model="form.isParty" placeholder="请选择对于帮扶同一个孩子的志愿者能否接受线下聚会">
            <el-option
              v-for="dict in whether_or_not"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <el-input v-model="form.picture" placeholder="请输入图片" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in business_information_zt"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Volunteer" lang="ts">
import { listVolunteer, getVolunteer, delVolunteer, addVolunteer, updateVolunteer } from '@/api/business/volunteer';
import { VolunteerVO, VolunteerQuery, VolunteerForm } from '@/api/business/volunteer/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { business_information_zt, whether_or_not, sys_user_sex } = toRefs<any>(proxy?.useDict('business_information_zt', 'whether_or_not', 'sys_user_sex'));

const volunteerList = ref<VolunteerVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const volunteerFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: VolunteerForm = {
  name: undefined,
  sex: undefined,
  age: undefined,
  nationality: undefined,
  tel: undefined,
  workUnit: undefined,
  politicalStatus: undefined,
  community: undefined,
  maritalStatus: undefined,
  childrenSex: undefined,
  childrenNum: undefined,
  isEnterprises: undefined,
  isVolunteer: undefined,
  isSociety: undefined,
  societyInfo: undefined,
  isTradition: undefined,
  traditionInfo: undefined,
  isBelief: undefined,
  belief: undefined,
  helpNeeds: undefined,
  helpNeedsInfo: undefined,
  isParty: undefined,
  picture: undefined,
  state: undefined
}
const data = reactive<PageData<VolunteerForm, VolunteerQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    sex: undefined,
    age: undefined,
    nationality: undefined,
    tel: undefined,
    workUnit: undefined,
    politicalStatus: undefined,
    community: undefined,
    maritalStatus: undefined,
    childrenSex: undefined,
    childrenNum: undefined,
    isEnterprises: undefined,
    isVolunteer: undefined,
    isSociety: undefined,
    societyInfo: undefined,
    isTradition: undefined,
    traditionInfo: undefined,
    isBelief: undefined,
    belief: undefined,
    helpNeeds: undefined,
    helpNeedsInfo: undefined,
    isParty: undefined,
    params: {
    }
  },
  rules: {
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    sex: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
    tel: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
    workUnit: [
      { required: true, message: "工作单位不能为空", trigger: "blur" }
    ],
    maritalStatus: [
      { required: true, message: "婚姻状况不能为空", trigger: "change" }
    ],
    isSociety: [
      { required: true, message: "是否加入过社会组织不能为空", trigger: "change" }
    ],
    isTradition: [
      { required: true, message: "是否对中华优秀传统文化感兴趣不能为空", trigger: "change" }
    ],
    traditionInfo: [
      { required: true, message: "哪本中华优秀传统文化书籍对自己有较大影响不能为空", trigger: "blur" }
    ],
    isBelief: [
      { required: true, message: "是否有信仰不能为空", trigger: "change" }
    ],
    belief: [
      { required: true, message: "信仰内容不能为空", trigger: "blur" }
    ],
    helpNeeds: [
      { required: true, message: "提供的帮助不能为空", trigger: "blur" }
    ],
    helpNeedsInfo: [
      { required: true, message: "帮助详情不能为空", trigger: "blur" }
    ],
    isParty: [
      { required: true, message: "对于帮扶同一个孩子的志愿者能否接受线下聚会不能为空", trigger: "change" }
    ],
    // picture: [
    //   { required: true, message: "图片不能为空", trigger: "blur" }
    // ],
    state: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询志愿者详细信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listVolunteer(queryParams.value);
  volunteerList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  volunteerFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: VolunteerVO[]) => {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加志愿者详细信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: VolunteerVO) => {
  reset();
  const _userId = row?.userId || ids.value[0]
  const res = await getVolunteer(_userId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改志愿者详细信息";
}

/** 提交按钮 */
const submitForm = () => {
  volunteerFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.userId) {
        await updateVolunteer(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addVolunteer(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: VolunteerVO) => {
  const _userIds = row?.userId || ids.value;
  await proxy?.$modal.confirm('是否确认删除志愿者详细信息编号为"' + _userIds + '"的数据项？').finally(() => loading.value = false);
  await delVolunteer(_userIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/volunteer/export', {
    ...queryParams.value
  }, `volunteer_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
